﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace ZyGame.Editor.UIBind
{
    class LayerData
    {
        public string name;
        public bool isGroup;
        public bool isEndGroup;
        public bool foldout;
        public Rect position;
        public Texture2D texture;
        public string text;
        public Font font;
        public LayerData parent;
        public bool selection;
        public List<LayerData> child = new List<LayerData>();
        private Vector3[] s_Corners = new Vector3[4];
        private Vector2 s_StartDragAnchorMin;

        private Vector2 s_StartDragAnchorMax;

        public Vector2 anchorMax;
        public Vector2 anchorMin;
        public Vector2 anchoredPosition;
        public Vector2 sizeDelta;
        public Vector2 pivot;
        public Rect rect;
        public Vector2 offsetMin;
        public Vector2 offsetMax;


        //private Vector3 GetRectReferenceCorner(bool worldSpace)
        //{
        //    if (worldSpace)
        //    {
        //        Transform transform = gui.transform;
        //        gui.GetWorldCorners(s_Corners);
        //        if ((bool)transform.parent)
        //        {
        //            return transform.parent.InverseTransformPoint(s_Corners[0]);
        //        }

        //        return s_Corners[0];
        //    }

        //    return (Vector3)this.rect.min + this.transform.localPosition;
        //}
        //public void SetPivotSmart(float value, int axis, bool smart, bool parentSpace)
        //{
        //    Vector3 rectReferenceCorner = GetRectReferenceCorner(rect, !parentSpace);
        //    Vector2 pivot = this.pivot;
        //    pivot[axis] = value;
        //    this.pivot = pivot;
        //    if (smart)
        //    {
        //        Vector3 rectReferenceCorner2 = GetRectReferenceCorner(rect, !parentSpace);
        //        Vector3 vector = rectReferenceCorner2 - rectReferenceCorner;
        //        anchoredPosition -= (Vector2)vector;
        //        Vector3 position = rect.transform.position;
        //        position.z -= vector.z;
        //        this.position = position;
        //    }
        //}

        public void SetAnchorSmart(float value, int axis, bool isMax, bool smart)
        {
            SetAnchorSmart(
                value,
                axis,
                isMax,
                smart,
                enforceExactValue: false,
                enforceMinNoLargerThanMax: false,
                moveTogether: false
            );
        }

        public void SetAnchorSmart(
            float value,
            int axis,
            bool isMax,
            bool smart,
            bool enforceExactValue
        )
        {
            SetAnchorSmart(
                value,
                axis,
                isMax,
                smart,
                enforceExactValue,
                enforceMinNoLargerThanMax: false,
                moveTogether: false
            );
        }

        public void SetAnchorSmart(
            float value,
            int axis,
            bool isMax,
            bool smart,
            bool enforceExactValue,
            bool enforceMinNoLargerThanMax,
            bool moveTogether
        )
        {
            if (parent == null)
            {
                smart = false;
            }

            bool flag = !AnchorAllowedOutsideParent(axis, isMax ? 1 : 0);
            if (flag)
            {
                value = Mathf.Clamp01(value);
            }

            if (enforceMinNoLargerThanMax)
            {
                value = (
                    (!isMax) ? Mathf.Min(value, anchorMax[axis]) : Mathf.Max(value, anchorMin[axis])
                );
            }

            float num = 0f;
            float num2 = 0f;
            if (smart)
            {
                float num3 = (isMax ? anchorMax[axis] : anchorMin[axis]);
                num = (value - num3) * parent.rect.size[axis];
                float num4 = 0f;
                //if (ShouldDoIntSnapping(rect))
                //{
                //    num4 = Mathf.Round(num) - num;
                //}

                num += num4;
                if (!enforceExactValue)
                {
                    value += num4 / parent.rect.size[axis];
                    if (Mathf.Abs(Round(value * 1000f) - value * 1000f) < 0.1f)
                    {
                        value = Round(value * 1000f) * 0.001f;
                    }

                    if (flag)
                    {
                        value = Mathf.Clamp01(value);
                    }

                    if (enforceMinNoLargerThanMax)
                    {
                        value = (
                            (!isMax)
                                ? Mathf.Min(value, anchorMax[axis])
                                : Mathf.Max(value, anchorMin[axis])
                        );
                    }
                }

                num2 = (
                    (!moveTogether)
                        ? (isMax ? (num * pivot[axis]) : (num * (1f - pivot[axis])))
                        : num
                );
            }

            if (isMax)
            {
                Vector2 anchorMax = this.anchorMax;
                anchorMax[axis] = value;
                this.anchorMax = anchorMax;
                Vector2 anchorMin = this.anchorMin;
                if (moveTogether)
                {
                    anchorMin[axis] =
                        s_StartDragAnchorMin[axis] + anchorMax[axis] - s_StartDragAnchorMax[axis];
                }

                this.anchorMin = anchorMin;
            }
            else
            {
                Vector2 anchorMin2 = anchorMin;
                anchorMin2[axis] = value;
                anchorMin = anchorMin2;
                Vector2 anchorMax2 = anchorMax;
                if (moveTogether)
                {
                    anchorMax2[axis] =
                        s_StartDragAnchorMax[axis] + anchorMin2[axis] - s_StartDragAnchorMin[axis];
                }

                anchorMax = anchorMax2;
            }

            if (smart)
            {
                Vector2 anchoredPosition = this.anchoredPosition;
                anchoredPosition[axis] -= num2;
                this.anchoredPosition = anchoredPosition;
                if (!moveTogether)
                {
                    Vector2 sizeDelta = this.sizeDelta;
                    sizeDelta[axis] += num * (float)((!isMax) ? 1 : (-1));
                    this.sizeDelta = sizeDelta;
                }
            }
        }

        private bool AnchorAllowedOutsideParent(int axis, int minmax)
        {
            if (EditorGUI.actionKey || GUIUtility.hotControl == 0)
            {
                return true;
            }

            float num = ((minmax == 0) ? s_StartDragAnchorMin[axis] : s_StartDragAnchorMax[axis]);
            return num < -0.001f || num > 1.001f;
        }

        //private bool ShouldDoIntSnapping(RectTransform rect)
        //{
        //    Canvas componentInParent = rect.gameObject.GetComponentInParent<Canvas>();
        //    return componentInParent != null && componentInParent.renderMode != RenderMode.WorldSpace;
        //}

        private float Round(float value)
        {
            return Mathf.Floor(0.5f + value);
        }

        private int RoundToInt(float value)
        {
            return Mathf.FloorToInt(0.5f + value);
        }
    }
}